package com.park.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.park.model.AuthUser;
import com.park.model.Constants;
import com.park.model.Feeoperatechargerecord;
import com.park.service.AuthorityService;
import com.park.service.FeeoperatorChargeRecordService;
import com.park.service.Utility;

@Controller
@RequestMapping("feeoperatorchargeRecord")
public class FeeoperatorChargeRecordController {

	@Autowired
	FeeoperatorChargeRecordService feeoperatorchargeRecordService;
	@Autowired
	AuthorityService authorityService;
	
	@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insert(@RequestBody Feeoperatechargerecord record) {
		int num=feeoperatorchargeRecordService.insertSelective(record);
		Map<String, Object> result=new HashMap<>();
		if (num==1) {
			result.put("status", 1001);
			result.put("message", "成功");
		}
		else {
			result.put("status", 1002);
			
		}
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value="getbyUserAndDate",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getbyUserAndDate(@RequestBody Map<String, Object> args) throws ParseException {
		String username=(String) args.get("userName");
		AuthUser user=authorityService.getUserByUsername(username);
		Map<String, Object> result=new HashMap<>();
		if (user==null) {
			result.put("status", 1002);
			result.put("message", "没有此用户");
			return Utility.gson.toJson(result);
		}
		String startTime=(String) args.get("startTime");
		String endTime=(String) args.get("endTime");
		int startPage=(int) args.get("startPage");
		int count=(int) args.get("count");
		PageHelper.startPage(startPage,count);
		List<Map<String, Object>> data=feeoperatorchargeRecordService.getbyUserAndDate(user.getId(), new SimpleDateFormat(Constants.DATEFORMAT).parse(startTime), new SimpleDateFormat(Constants.DATEFORMAT).parse(endTime));
		 PageInfo pageInfo = new PageInfo<>(data);
		
			result.put("status", 1001);
			result.put("body", pageInfo);
		
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value="getByOperatorAndDate",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByOperatorAndDate(@RequestBody Map<String, Object> args) throws ParseException {
		String username=(String) args.get("userName");
		AuthUser user=authorityService.getUserByUsername(username);
		Map<String, Object> result=new HashMap<>();
		if (user==null) {
			result.put("status", 1002);
			result.put("message", "没有此用户");
			return Utility.gson.toJson(result);
		}
		String startTime=(String) args.get("startTime");
		String endTime=(String) args.get("endTime");
		int startPage=(int) args.get("startPage");
		int count=(int) args.get("count");
		PageHelper.startPage(startPage,count);
		List<Map<String, Object>> data=feeoperatorchargeRecordService.getByOperatorAndDate(user.getId(), new SimpleDateFormat(Constants.DATEFORMAT).parse(startTime), new SimpleDateFormat(Constants.DATEFORMAT).parse(endTime));
		 PageInfo pageInfo = new PageInfo<>(data);
		
			result.put("status", 1001);
			result.put("body", pageInfo);
		
		return Utility.gson.toJson(result);
	}
}
